/*
 *Copyright 2011 Digia Oyj - Pasi Hirvonen <pasi.hirvonen@digia.fi>
 */

#ifndef RPCPARSER_H
#define RPCPARSER_H

#include <QtCore/QList>
#include <QtCore/QMap>
#include <QtCore/QVariant>

class QString;
class QDomElement;

namespace XmlRpc {

class XmlRpcParserContentsPrivate;
class XmlRpcParserContents {
public:
    XmlRpcParserContents();
    ~XmlRpcParserContents();

    QVariant params() const;
    bool isFault() const;
    int faultCode() const;
    QString faultString() const;

protected:
    void setFault(int faultCode, const QString &faultString);
    void setParams(const QVariant &params);
private:
    XmlRpcParserContentsPrivate *d;
    friend class XmlRpcParser;
};

class XmlRpcParser
{
public:
    /*
     * Construct a new XML-RPC parser.
     */
    XmlRpcParser();

    // Parses the given parameter list to a valid <params> tag
    // for methodCall
    QString parseToString(const QList<QVariant> &params);

    // Parses a complete RpcResponse (string must be a valid, complete
    // XML document)
    XmlRpcParserContents parseRpcResponse(const QString &string);

    // Parses a single <param> tag into a QVariant
    QVariant parseParam(const QDomElement &paramRoot);
private:
    QVariant parseValue(const QDomElement &element);
    QMap<QString, QVariant> parseStruct(const QDomElement &structElement);
    QList<QVariant> parseList(const QDomElement &arrayElement);
    bool parseError(const QDomElement &paramRoot, int *errorCode, QString *errorString);

    QMap<QString, QVariant::Type> m_valueMap;
    Q_DISABLE_COPY(XmlRpcParser)
};

} // namespace XmlRpc

#endif // RPCPARSER_H
